function WP_OnEvent()
	if event == "ADDON_LOADED" then WP_SetGuide(WP_currentType, WP_currentGuideID, WP_currentStep) end

	if event == "CHAT_MSG_SYSTEM" then
		if arg1 then _,_,questg = string.find(arg1, "Quest accepted: ([%a%d%s'!:%-]*)") end
		if questg then WP_CheckQuestGetEvent(questg) end
		
		if arg1 then _,_,questt = string.find(arg1, "([%a%d%s'!:%-]*) completed.") end
		if questt then WP_CheckQuestTurnEvent(questt) end
		
	elseif event == "CHAT_MSG_LOOT" then 
		if arg1 then _,_,loot = string.find(arg1, "%[([%a%d%s'!:%-]*)%]") end
		if loot then WP_CheckLootEvent(loot) end
		
	elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
		if arg4 == UnitName("player") and arg2 == "PARTY_KILL" then WP_CheckKillEvent(arg7) end
		
	elseif event == "QUEST_LOG_UPDATE" then
		WP_CheckQuestEvent()	
		
	elseif event == "ZONE_CHANGED" then
		WP_CheckZoneEvent()
	end
end 


function WP_RegisterStepEvents(msg)
	msg = WP_RegisterQuest(msg)
	msg = WP_RegisterQuestGetEvent(msg)
	msg = WP_RegisterQuestTurnEvent(msg)
	msg = WP_RegisterCoordsEvent(msg)
	msg = WP_RegisterLootEvent(msg)
	msg = WP_RegisterKillEvent(msg)
	msg = WP_RegisterZoneEvent(msg)
end 

---------------------------- QUEST EVENT ----------------------------

function WP_RegisterQuest(msg)
	local _,_,f1,f2 = string.find(msg, "<regQuest=(.-)=(%d)>")
	if f1 and f2 then
		table.insert(WP_currentEvents,{"Q2",f1,f2,false})
		msg = string.gsub(msg, "<regQuest=.-="..f2..">","",1)
		f1,f2 = nil
		WP_RegisterQuest(msg)
	end

	local _,_,f1 = string.find(msg, "<regQuest=(.-)>")
	if f1 then
		table.insert(WP_currentEvents,{"Q",f1,false})
		msg = string.gsub(msg, "<regQuest=.->","",1)
		f1 = nil
		WP_RegisterQuest(msg)
	end
	return msg
end

function WP_CheckQuestEvent()
	local numEntries = GetNumQuestLogEntries()
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "Q" then
			for i = 1, numEntries, 1 do
				local title,_,_,_,_,_,complete = GetQuestLogTitle(i)		
				if complete and title == v[2] then
					v[3] = true
					WP_CheckAllEvents()
				end
			end
			
		elseif v[1] == "Q2" then
			for i = 1, numEntries, 1 do
				local title = GetQuestLogTitle(i)
				if title == v[2] then
					local _,_, done = GetQuestLogLeaderBoard (v[3],i)
					if done then 
						v[4] = true
						WP_CheckAllEvents()
					end
				end	 -- if title == v[2] then			
			end -- for i = 1, numEntries, 1 do
		end -- elseif v[1] == "Q2" then
	end -- for k,v in pairs(WP_currentEvents) do
end


----------------------------- QUEST GET EVENT -------------------------

function WP_RegisterQuestGetEvent(msg)
	local _,_,f1 = string.find(msg, "<regGet=(.-)>")
	if f1 ~= nil then 
		table.insert(WP_currentEvents,{"G",f1,false})
		msg,num = string.gsub(msg, "<regGet=.->","",1)
		WP_RegisterQuestGetEvent(msg)
		f1 = nil
	end
	return msg
end 

function WP_CheckQuestGetEvent(quest)
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "G" and v[2] == quest then
			v[3] = true
			WP_CheckAllEvents()
		end
	end
end 

----------------------------- QUEST TURN IN EVENT -------------------------

function WP_RegisterQuestTurnEvent(msg)
	local _,_,f1 = string.find(msg, "<regTurn=(.-)>")
	if f1 then 
		table.insert(WP_currentEvents,{"T",f1,false})
		msg = string.gsub(msg, "<regTurn=.->","",1)
		f1 = nil
		WP_RegisterQuestTurnEvent(msg)
	end
	return msg
end 

function WP_CheckQuestTurnEvent(quest)
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "T"  and v[2] == quest then
			v[3] = true
			WP_CheckAllEvents()
		end
	end
end 

----------------------------- COORDS EVENT -------------------------

function WP_RegisterCoordsEvent(msg)
	if string.find(msg, "<regCoords>") then 
		table.insert(WP_currentEvents,{"C",false})
		msg = string.gsub(msg, "<regCoords>", "",1)
	end
	return msg
end 

function WP_CheckCoordsEvent()
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "C" then
			v[2] = true;
			WP_CheckAllEvents()
		end
	end
end 

----------------------------- LOOT EVENT -------------------------

function WP_RegisterLootEvent(msg)
	_,_,f1,f2 = string.find(msg, "<regLoot=(.-)=(%d*)>")
	if f1 and f2 then 
		table.insert(WP_currentEvents,{"L",f1,tonumber(f2),false})
		msg = string.gsub(msg, "<regLoot=.-="..f2..">", "", 1)
		f1,f2 = nil
		WP_RegisterLootEvent(msg)
	end
	return msg
end 

function WP_CheckLootEvent(loot)
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "L" and v[2] == loot and GetItemCount(loot)+1 >= v[3] then
			v[4] = true
			WP_CheckAllEvents()
		end
	end
end 

----------------------------- KILL EVENT -------------------------

function WP_RegisterKillEvent(msg)
	local _,_,f1,f2 = string.find(msg, "<regKill=(.-)=(%d*)>")
	if f1 and f2 then
		table.insert(WP_currentEvents,{"K",f1,tonumber(f2),0,false})
		msg = string.gsub(msg, "<regKill=.-="..f2..">","",1)
		f1,f2 = nil
		WP_RegisterKillEvent(msg)
	end
	return msg
end 

function WP_CheckKillEvent(dead)
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "K" and v[2] == dead then
			v[4] = v[4] + 1
			if v[4] >= v[3] then
				v[5] = true
				WP_CheckAllEvents()
			end
		end
	end
end 

----------------------------- ZONE EVENT -------------------------

function WP_RegisterZoneEvent(msg)
	local _,_,f1 = string.find(msg, "<regZone=(.-)>")
	if f1 then 
		table.insert(WP_currentEvents,{"Z",f1,false})
		msg = string.gsub(msg, "<regZone=.->","",1)
		f1 = nil
		WP_RegisterZoneEvent(msg)
	end
	return msg
end 

function WP_CheckZoneEvent()
	local zone = GetSubZoneText()
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "Z"  and v[2] == zone then
			v[3] = true
			WP_CheckAllEvents()
		end
	end
end 

-----------------------------------------------------

function WP_CheckAllEvents()
	local gonext = true
	
	for k,v in pairs(WP_currentEvents) do
		if v[1] == "Q" and v[3] == false then gonext = false end
		if v[1] == "Q2" and v[4] == false then gonext = false end
		if v[1] == "K" and v[5] == false then gonext = false end
		if v[1] == "L" and v[4] == false then gonext = false end
		if v[1] == "G" and v[3] == false then gonext = false end
		if v[1] == "T" and v[3] == false then gonext = false end
		if v[1] == "C" and v[2] == false then gonext = false end
		if v[1] == "Z" and v[3] == false then gonext = false end
	end
	
	if gonext then WP_NextStep() end
end 